package com.hq.fxh.No1_Infromation.Infor.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hq.fxh.Modle.SelectByCategoryIdRes;
import com.hq.fxh.R;
import com.hq.fxh.Utils.Tool;

import java.util.List;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/28
 * @Email xlwenhao1858@sina.com
 */


public class Ladapter_infor extends BaseAdapter {
    List<SelectByCategoryIdRes.ValueBean> data;
    InforCallBack inforCallBack;

    public void setData(List<SelectByCategoryIdRes.ValueBean> data) {
        this.data = data;

    }

    public Ladapter_infor(InforCallBack inforCallBack) {
        this.inforCallBack = inforCallBack;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int i) {

        return data == null ? null : data.get(i);

    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final SelectByCategoryIdRes.ValueBean valueBean = data.get(position);

        switch (getItemViewType(position)) {

            case 1:

                NewsType1ViewHolder viewHolder = null;
                if (view == null || !(view.getTag() instanceof NewsType1ViewHolder)) {
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_infor_news1, null);
                    viewHolder = new NewsType1ViewHolder(view);
                    view.setTag(viewHolder);
                } else {
                    viewHolder = (NewsType1ViewHolder) view.getTag();
                }
                String s = valueBean.getImage().get(0);
                Tool.setUrlToImage(parent.getContext(), s, viewHolder.newImageView);
                Tool.setStringToText(viewHolder.textTitle, valueBean.getTitle());
                viewHolder.newsType1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        inforCallBack.onClickOpenDetail(position, valueBean);
                    }
                });
                setOnListener(viewHolder.pingbi, position, valueBean);
                return view;


            case 2:
                NewsType2ViewHolder viewHolder2 = null;

                if (view == null || !(view.getTag() instanceof NewsType2ViewHolder)) {
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_infor_news2, null);
                    viewHolder2 = new NewsType2ViewHolder(view);
                    view.setTag(viewHolder2);
                } else {
                    viewHolder2 = (NewsType2ViewHolder) view.getTag();
                }

                Tool.setUrlToImage(parent.getContext(), valueBean.getImage().get(0), viewHolder2.newsImage1);
                Tool.setUrlToImage(parent.getContext(), valueBean.getImage().get(1), viewHolder2.newsImage2);
                Tool.setUrlToImage(parent.getContext(), valueBean.getImage().get(2), viewHolder2.newsImage3);

                Tool.setStringToText(viewHolder2.textTitle, valueBean.getTitle());
                viewHolder2.newsType2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        inforCallBack.onClickOpenDetail(position, valueBean);
                    }
                });
                setOnListener(viewHolder2.pingbi, position, valueBean);

                return view;


            case 3:
                NewsType3ViewHolder viewHolder3 = null;

                if (view == null || !(view.getTag() instanceof NewsType3ViewHolder)) {
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_infor_news3, null);
                    viewHolder3 = new NewsType3ViewHolder(view);
                    view.setTag(viewHolder3);
                } else {
                    viewHolder3 = (NewsType3ViewHolder) view.getTag();
                }
                viewHolder3.newsType3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        inforCallBack.onClickOpenDetail(position, valueBean);

                    }
                });
                setOnListener(viewHolder3.pingbi, position, valueBean);
                return view;

            default:
                NewsType4ViewHolder viewHolder4 = null;

                if (view == null || !(view.getTag() instanceof NewsType4ViewHolder)) {
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_infor_news4, null);
                    viewHolder4 = new NewsType4ViewHolder(view);
                    view.setTag(viewHolder4);
                } else {
                    viewHolder4 = (NewsType4ViewHolder) view.getTag();
                }
                return view;


        }


    }

    @Override
    public int getItemViewType(int position) {
        int type = -1;
        if (data == null) {
            type = 4;
        } else {
            int size = data.get(position).getImage().size();
            if (size == 1 || size == 2) {
                type = 1;
            } else if (size == 3) {
                type = 2;
            } else if (size == 0) {
                type = 3;
            }
        }
        return type;
    }

    public void setOnListener(View view, final int position, final SelectByCategoryIdRes.ValueBean data) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inforCallBack.onClickPingBi(view, position, data);
            }
        });
    }


    public class NewsType1ViewHolder {

        protected TextView textTitle;
        protected LinearLayout newsType1LinyoutAD;
        protected ImageView authorIcon;
        protected TextView authorName;
        protected TextView newsTag;
        protected LinearLayout NewsType1LinyoutAuthor;
        protected TextView copyRightName;
        protected LinearLayout NewsType1LinyoutCopyRight;
        protected TextView readNewNum;
        protected LinearLayout newsType1LinyoutNoAD;
        protected LinearLayout newsType1;

        protected ImageView newImageView;
        protected LinearLayout news_author_tag;
        protected LinearLayout pingbi;

        public NewsType1ViewHolder(View itemView) {
            initView(itemView);
        }

        private void initView(View rootView) {
            textTitle = (TextView) rootView.findViewById(R.id.text_title);
            newsType1LinyoutAD = (LinearLayout) rootView.findViewById(R.id.newsType1_Linyout_AD);
            authorIcon = (ImageView) rootView.findViewById(R.id.author_icon);
            authorName = (TextView) rootView.findViewById(R.id.author_name);
            newsTag = (TextView) rootView.findViewById(R.id.news_tag);
            NewsType1LinyoutAuthor = (LinearLayout) rootView.findViewById(R.id.NewsType1_Linyout_author);
            copyRightName = (TextView) rootView.findViewById(R.id.copyRight_name);
            NewsType1LinyoutCopyRight = (LinearLayout) rootView.findViewById(R.id.NewsType1_Linyout_CopyRight);
            readNewNum = (TextView) rootView.findViewById(R.id.readNewNum);
            newsType1LinyoutNoAD = (LinearLayout) rootView.findViewById(R.id.newsType1_Linyout_NoAD);
            newsType1 = (LinearLayout) rootView.findViewById(R.id.newsType1);
            news_author_tag = (LinearLayout) rootView.findViewById(R.id.news_author_tag);
            pingbi = (LinearLayout) rootView.findViewById(R.id.pingbi);
            newImageView = (ImageView) rootView.findViewById(R.id.new_imageView);
        }


    }

    public class NewsType2ViewHolder {
        protected TextView textTitle;
        protected LinearLayout newsType2LinyoutAD;
        protected ImageView authorIcon;
        protected ImageView newsImage1;
        protected ImageView newsImage2;
        protected ImageView newsImage3;
        protected TextView authorName;
        protected TextView newsTag;
        protected LinearLayout NewsType2LinyoutAuthor;
        protected TextView copyRightName;
        protected LinearLayout NewsType2LinyoutCopyRight;
        protected TextView readNewNum;
        protected LinearLayout newsType2LinyoutNoAD;
        protected LinearLayout newsType2;
        protected LinearLayout news_author_tag;
        protected LinearLayout pingbi;

        public NewsType2ViewHolder(View itemView) {
            initView(itemView);
        }

        public void initView(View rootView) {
            newsType2 = (LinearLayout) rootView.findViewById(R.id.newsType2);

            textTitle = (TextView) rootView.findViewById(R.id.text_title);
            newsType2LinyoutAD = (LinearLayout) rootView.findViewById(R.id.newsType2_Linyout_AD);
            authorIcon = (ImageView) rootView.findViewById(R.id.author_icon);
            authorName = (TextView) rootView.findViewById(R.id.author_name);
            newsTag = (TextView) rootView.findViewById(R.id.news_tag);
            NewsType2LinyoutAuthor = (LinearLayout) rootView.findViewById(R.id.NewsType2_Linyout_author);
            copyRightName = (TextView) rootView.findViewById(R.id.copyRight_name);
            NewsType2LinyoutCopyRight = (LinearLayout) rootView.findViewById(R.id.NewsType2_Linyout_CopyRight);
            readNewNum = (TextView) rootView.findViewById(R.id.readNewNum);
            newsType2LinyoutNoAD = (LinearLayout) rootView.findViewById(R.id.newsType2_Linyout_NoAD);
            newsImage1 = (ImageView) rootView.findViewById(R.id.news_image1);
            newsImage2 = (ImageView) rootView.findViewById(R.id.news_image2);
            newsImage3 = (ImageView) rootView.findViewById(R.id.news_image3);
            news_author_tag = (LinearLayout) rootView.findViewById(R.id.news_author_tag);
            pingbi = (LinearLayout) rootView.findViewById(R.id.pingbi);
       /*     float windowWidth = (float) SystemUtils.getWindowWidth((Activity) mcontext);
            int i = DensityUtils.dip2px(mcontext, 40);
            int viewWidth = (int) (windowWidth - i) / 3;
            setImageWH(newsImage1, viewWidth, (viewWidth / 4) * 3);
            setImageWH(newsImage2, viewWidth, (viewWidth / 4) * 3);
            setImageWH(newsImage3, viewWidth, (viewWidth / 4) * 3);*/
        }
    }

    public void setImageWH(ImageView image, int width, int height) {
        ViewGroup.LayoutParams layoutParams = image.getLayoutParams();
        layoutParams.height = height;
        layoutParams.width = width;
        image.setLayoutParams(layoutParams);

    }

    public class NewsType3ViewHolder {
        protected TextView textTitle;
        protected LinearLayout newsType3LinyoutAD;
        protected ImageView authorIcon;
        protected TextView authorName;
        protected TextView newsTag;
        protected LinearLayout NewsType3LinyoutAuthor;
        protected TextView copyRightName;
        protected LinearLayout NewsType3LinyoutCopyRight;
        protected TextView readNewNum;
        protected LinearLayout newsType3LinyoutNoAD;
        protected LinearLayout newsType3;
        protected LinearLayout news_author_tag;
        protected LinearLayout pingbi;


        public NewsType3ViewHolder(View itemView) {
            initView(itemView);
        }

        public void initView(View rootView) {
            newsType3 = (LinearLayout) rootView.findViewById(R.id.newsType3);
            news_author_tag = (LinearLayout) rootView.findViewById(R.id.news_author_tag);
            pingbi = (LinearLayout) rootView.findViewById(R.id.pingbi);
            textTitle = (TextView) rootView.findViewById(R.id.text_title);
            newsType3LinyoutAD = (LinearLayout) rootView.findViewById(R.id.newsType3_Linyout_AD);
            authorIcon = (ImageView) rootView.findViewById(R.id.author_icon);
            authorName = (TextView) rootView.findViewById(R.id.author_name);
            newsTag = (TextView) rootView.findViewById(R.id.news_tag);
            NewsType3LinyoutAuthor = (LinearLayout) rootView.findViewById(R.id.NewsType3_Linyout_author);
            copyRightName = (TextView) rootView.findViewById(R.id.copyRight_name);
            NewsType3LinyoutCopyRight = (LinearLayout) rootView.findViewById(R.id.NewsType3_Linyout_CopyRight);
            readNewNum = (TextView) rootView.findViewById(R.id.readNewNum);
            newsType3LinyoutNoAD = (LinearLayout) rootView.findViewById(R.id.newsType3_Linyout_NoAD);
        }
    }

    public class NewsType4ViewHolder {

        protected TextView textTitle;
        protected ImageView newImageView;
        protected LinearLayout newsType4LinyoutAD;
        protected ImageView authorIcon;
        protected TextView authorName;
        protected TextView newsTag;
        protected LinearLayout NewsType4LinyoutAuthor;
        protected TextView copyRightName;
        protected LinearLayout NewsType4LinyoutCopyRight;
        protected TextView readNewNum;
        protected LinearLayout newsType4LinyoutNoAD;
        protected LinearLayout newsType4;

        public NewsType4ViewHolder(View itemView) {
            initView(itemView);
        }

        public void initView(View rootView) {
            newsType4 = (LinearLayout) rootView.findViewById(R.id.newsType4);

            textTitle = (TextView) rootView.findViewById(R.id.text_title);
            newImageView = (ImageView) rootView.findViewById(R.id.new_imageView);
            newsType4LinyoutAD = (LinearLayout) rootView.findViewById(R.id.newsType4_Linyout_AD);
            authorIcon = (ImageView) rootView.findViewById(R.id.author_icon);
            authorName = (TextView) rootView.findViewById(R.id.author_name);
            newsTag = (TextView) rootView.findViewById(R.id.news_tag);
            NewsType4LinyoutAuthor = (LinearLayout) rootView.findViewById(R.id.NewsType4_Linyout_author);
            copyRightName = (TextView) rootView.findViewById(R.id.copyRight_name);
            NewsType4LinyoutCopyRight = (LinearLayout) rootView.findViewById(R.id.NewsType4_Linyout_CopyRight);
            readNewNum = (TextView) rootView.findViewById(R.id.readNewNum);
            newsType4LinyoutNoAD = (LinearLayout) rootView.findViewById(R.id.newsType4_Linyout_NoAD);
    /*        float windowWidth = (float) SystemUtils.getWindowWidth((Activity) mcontext);
            int i = DensityUtils.dip2px(mcontext, 30);
            int viewWidth = (int) (windowWidth - i);
            setImageWH(newImageView, viewWidth, (viewWidth / 2));*/

        }
    }

    public interface InforCallBack {
        void onClickPingBi(View view, int position, SelectByCategoryIdRes.ValueBean data);

        void onClickOpenDetail(int position, SelectByCategoryIdRes.ValueBean data);
    }

}

